﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using IOEntity;

namespace IODataAccess
{
    public class clsDADetail
    {
        clsDAConnection con = new clsDAConnection();
        public clsDetail GetById(int id)
        {
            try
            {
                clsDetail obj = new clsDetail();
                obj.TypeDetail = new clsTypeDetail();
                obj.Tour = new clsTour();
                string str = "select * from tblDetail where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    obj.Id = id;
                    
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["tourid"] != DBNull.Value)
                    {
                        obj.Tour.Id = Convert.ToInt32(dr["tourid"].ToString());
                    }
                    if (dr["typedetailid"] != DBNull.Value)
                    {
                        obj.TypeDetail.Id = Convert.ToInt32(dr["typedetailid"].ToString());
                    }
                }
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsDetail> GetByTour(int tourid)
        {
            try
            {
                List<clsDetail> list = new List<clsDetail>();
                string str = "select * from tblDetail where tourid=@tourid";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tourid", tourid);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsDetail obj = new clsDetail();
                    obj.TypeDetail = new clsTypeDetail();
                    obj.Tour = new clsTour();
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["tourid"] != DBNull.Value)
                    {
                        obj.Tour.Id = Convert.ToInt32(dr["tourid"].ToString());
                    }
                    if (dr["typedetailid"] != DBNull.Value)
                    {
                        obj.TypeDetail.Id = Convert.ToInt32(dr["typedetailid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsDetail> GetByLoai(int loai)
        {
            try
            {
                List<clsDetail> list = new List<clsDetail>();
                string str = "select * from tblDetail where typedetailid=@typedetailid";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@typedetailid", loai);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsDetail obj = new clsDetail();
                    obj.TypeDetail = new clsTypeDetail();
                    obj.Tour = new clsTour();
                    if (dr["noidung"] != DBNull.Value)
                    {
                        obj.NoiDung = dr["noidung"].ToString();
                    }
                    if (dr["tourid"] != DBNull.Value)
                    {
                        obj.Tour.Id = Convert.ToInt32(dr["tourid"].ToString());
                    }
                    if (dr["typedetailid"] != DBNull.Value)
                    {
                        obj.TypeDetail.Id = Convert.ToInt32(dr["typedetailid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public int Insert(clsDetail obj)
        {
            try
            {
                string str = "insert into tblDetail(noidung,tourid,typedetailid) values(@noidung,@tourid,@typedetailid)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@noidung", obj.NoiDung);
                com.Parameters.AddWithValue("@tourid", obj.Tour.Id);
                com.Parameters.AddWithValue("@typedetailid", obj.TypeDetail.Id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
                return clsDAStaticMethod.getIDIdentity("tblDetail");
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Update(clsDetail obj)
        {
            try
            {
                string str = "Update tblDetail set noidung=@noidung,tourid=@tourid,typedetailid=@typedetailid where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@noidung", obj.NoiDung);
                com.Parameters.AddWithValue("@tourid", obj.Tour.Id);
                com.Parameters.AddWithValue("@typedetailid", obj.TypeDetail.Id);
                com.Parameters.AddWithValue("@id", obj.Id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Delete(int id)
        {
            try
            {
                string str = "delete from tblDetail where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
    }
}
